package cn.biplam.back.services.database;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import cn.biplam.back.jda.Jda;
import cn.biplam.common.bean.sqlengine.Sql;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.interfaces.DatabaseServer;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;


/**
 * 本类主要用于数据库信息的查询、修改、删除、插入等操作，
 *   根据传入的DbResource的参数信息进行判断，如果是默认
 *   的数据源_BI_Dev 、_BI_Run_Manage、_BI_Run_App，
 *   则用默认的数据库连接池的方式取得连接，进行数据库操作。
 *   如果不是默认连接池，则用指定的数据源信息，根据不同的数据库类型，
 *   数据库版本取得相应的数据库jdbc连接，然后进行数据库操作。
 * @author WangSong Feb 27, 2009 10:24:31 AM
 * @version 1.0
 */
public class DatabaseServerImpl extends UnicastRemoteObject implements DatabaseServer {

	private static final long serialVersionUID = -3953923195808208937L;
	private Lock mLock=new ReentrantLock();
	private int iCount=0;

	/**
	 * 构造方法
	 * @throws RemoteException
	 */
	public DatabaseServerImpl() throws RemoteException {
		iCount++;
	}

	/**
	 * 	   数据库查询
	 * @param  mSql  sql操作类
	 * @param  mDbResource	数据源信息
	 * @param  iQueryModel	分页开关
	 * @param  sUserID		用户id
	 * @return Connection	数据库连接
	 * @throws RemoteException
	 */
	public SqlResult  executeSelect(Sql mSql,DbResource mDbResource,int iQueryModel,String sUserID,String sPageID) throws RemoteException,ErrorMessage {

		SqlResult sr = null;
		Jda mJda = new Jda(mDbResource,iQueryModel,sUserID,sPageID);
		sr = mJda.executeSelect(mSql);
		return sr;
	}
	
	/**
	 * 	   数据库查询
	 * @param  mSql  sql操作类
	 * @param  mDbResource	数据源信息
	 * @param  iQueryModel	分页开关
	 * @param  sUserID		用户id
	 * @return Connection	数据库连接
	 * @throws RemoteException
	 */
	public SqlResult  executeSelect(Sql mSql,DbResource mDbResource,int iQueryModel,String sUserID,String sPageID,int resultSize) throws RemoteException,ErrorMessage {

		SqlResult sr = null;
		Jda mJda = new Jda(mDbResource,iQueryModel,sUserID,sPageID);
		sr = mJda.executeSelect(mSql,resultSize);
		return sr;
	}
	
	/**
	 * 	   数据库查询
	 * @param  mSql  sql操作类
	 * @param  mDbResource	数据源信息
	 * @param  iQueryModel	分页开关
	 * @param  sUserID		用户id
	 * @return Connection	数据库连接
	 * @throws RemoteException
	 */
	public SqlResult  executeSelect(Sql mSql,DbResource mDbResource,int iQueryModel,String sUserID,String sPageID,int start,int end) throws RemoteException,ErrorMessage {

		SqlResult sr = null;
		Jda mJda = new Jda(mDbResource,iQueryModel,sUserID,sPageID);
		sr = mJda.executeSelect(mSql,start,end);
		return sr;
	}

	/**
	 * 执行 create  drop 等不能rollback的 sql的方法
	 * @param  mSql  sql操作类
	 * @param  mDbResource	数据源信息
	 * @return boolean
	 */
	public boolean executeDDL (Sql mSql,DbResource mDbResource) throws RemoteException,ErrorMessage {

		boolean boo = true;
		addCount();
		try{
			Jda mJda = new Jda(mDbResource);
			boo = mJda.executeDDL(mSql);
		}finally{
			reduceCount();
		}
		return boo;
	}

	/**
	 * 执行 delete  update insert等 可以rollback的 sql的方法
	 * @param  mSql  sql操作类
	 * @param  mDbResource	数据源信息
	 * @return boolean
	 */
	public boolean executeDML (Sql mSql,DbResource mDbResource) throws RemoteException,ErrorMessage {

		boolean boo = true;
		addCount();
		try{
			Jda mJda = new Jda(mDbResource);
			boo = mJda.executeDML(mSql);
		}finally{
			reduceCount();
		}
		return boo;
	}

	/**
	 * 批量更新
	 * @param sql			SQL语句集合
	 * @param  mDbResource	数据源信息
	 * @return boolean
	 */
	public boolean executeDMLBatch(String [] sql,DbResource mDbResource) throws RemoteException,ErrorMessage {

		boolean boo = true;
		addCount(); 
		try{
			Jda mJda = new Jda(mDbResource);
			boo = mJda.executeDMLBatch(sql);
		}finally{
			reduceCount();
		}
		return boo;	
	}
	
	/**
	 * update batch
	 * @param sql List
	 * @return boolean
	 */
	@SuppressWarnings("finally")
	public boolean executeDMLBatch(List<String> sql,DbResource mDbResource) throws RemoteException,ErrorMessage{
		
		boolean boo = true;
		addCount(); 
		try{
			Jda mJda = new Jda(mDbResource);
			boo = mJda.executeDMLBatch(sql);
		}finally{
			reduceCount();
		}
		return boo;	
		
	}

	public String test(String text) throws RemoteException{
		return text;
	}

	public int getCount() throws RemoteException {
		return this.iCount;
	}

	private synchronized void addCount() {
		this.iCount++;
	}

	private synchronized void reduceCount() {
		this.iCount--;
	}

	public void lock() throws RemoteException {
		mLock.lock();
	}

	public boolean trylock() throws RemoteException {
		return mLock.tryLock();
	}

	public void unlock() {
		mLock.unlock();
	}


}
